<?php

namespace services\notices;

use think\Request;
use think\Response;
use think\Session;
use EasyWeChat\Foundation\Application;

class Wx implements NoticeInterface
{
    private $notice = '';

    public $options = [
            /**
             * Debug 模式，bool 值：true/false
             *
             * 当值为 false 时，所有的日志都不会记录
             */
            'debug'  => false,
            /**
             * 账号基本信息，请从微信公众平台/开放平台获取
             */
            'app_id'  => 'wx5a1090e16a2f3e95',         // AppID
            'secret'  => '7d7f576e91c8e8dc1af4fbeb6938ac2a',     // AppSecret
            'token'   => 'i9ch3f2ca',          // Token
            'aes_key' => 'RBuTcKRnHXbrLr4mrjQJW16ezM0UlggeHJZfu09ecnf',                    // EncodingAESKey，安全模式下请一定要填写！！！
            /**
             * Guzzle 全局设置
             *
             * 更多请参考： http://docs.guzzlephp.org/en/latest/request-options.html
             */
            'guzzle' => [
                'timeout' => 3.0, // 超时时间（秒）
                //'verify' => false, // 关掉 SSL 认证（强烈不建议！！！）
            ],
        ];

    private $templates = [
            'quit_deposits' => 'FdXX-frnAXM-3L9AaRuYZ6z9rGb9Tlbatwy9c_kPVys',
            'recharge'      => 'qF4scfrnIZ8D6kJ-gTS2oqdCZ95Xk7j16bmZIlmLPjo',
            'ordercreate'   => 'tZ8VLtV5BehUvO58FzqeQj1VYJQAe56HqQESOSAOoQo',
            'wishcreate'    => 'onU0ZmRYGjoyi8IxYFeqWeaY59hpFVVMaA-nICHFfIw'
        ];

    private $templateid = '';

	public function __construct($pimple)
    {
        $pimple->wx = $this;
        $app = new Application(array_merge($this->options, $pimple->options));
        $this->notice = $app->notice;
    }

    public function parseData($data)
    {
        if (key($data) === 0) {
            # code...
        }
        return $data;
    }

    public function send($open_id, $templateid, $data, $url = null)
    {
        $this->notice->send([
            'touser'      => $open_id,
            'template_id' => $this->templates[$templateid],
            'url'         => $url,
            'topcolor'    => '#f7f7f7',
            'data'        => $this->parseData($data),
        ]);       
    }
}
